//
//  DTodayHeaderView.swift
//  Drop
//
//  Created by hushaohua on 2018/1/16.
//  Copyright © 2018年 init. All rights reserved.
//

import UIKit

class DTodayHeaderView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame);
        self.addSubview(self.lineView);
        self.addSubview(self.beginDateLabel);
        self.addSubviewConstraints();
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var lineView:UIView = {
        var view:UIView = UIView(frame: .zero);
        view.translatesAutoresizingMaskIntoConstraints = false;
        for index in 0..<3 {
            let layer = self.dotLayer(CGRect(x:0,y:(4 + index * 10),width:6,height:6));
            view.layer.addSublayer(layer);
            self.lineDots.append(layer);
        }
        view.layer.addSublayer(self.dotLayer(CGRect(x:0,y:34,width:6,height:10)));
        return view;
    }();
    
    lazy var lineDots:[CALayer] = {
        return [CALayer]();
    }();
    
    func dotLayer(_ frame:CGRect) -> CALayer{
        let layer:CALayer = CALayer();
        layer.backgroundColor = UIColor.darkGray.cgColor;
        layer.frame = frame;
        return layer;
    }
    
    func startBright() -> Void{
        
        for index in 0..<self.lineDots.count {
            let layer = self.lineDots[index];
            let anim = CABasicAnimation(keyPath: "opacity");
            anim.fromValue = 1;
            anim.toValue = 0;
            anim.duration = 1;
            anim.repeatCount = MAXFLOAT;
            anim.autoreverses = true;
            anim.beginTime = CACurrentMediaTime() + TimeInterval(self.lineDots.count - index) * 0.5;
            layer.add(anim, forKey: "fade\(index)");
        }
    }
    
    lazy var beginDateLabel:UILabel = {
        var label:UILabel = UILabel(frame: .zero);
        label.translatesAutoresizingMaskIntoConstraints = false;
        label.textColor = UIColor.gray;
        label.font = UIFont.systemFont(ofSize: 16, weight: .medium);
        label.textAlignment = .left;
        return label;
    }();
    
    func addSubviewConstraints() -> Void{
        let views:[String : UIView] = ["time" : self.beginDateLabel, "line" : self.lineView];
        var costraints = [NSLayoutConstraint]();
        costraints += NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[line(==line_height)]", options: [.directionLeadingToTrailing], metrics: ["line_height" : 44], views: views);
        costraints += NSLayoutConstraint.constraints(withVisualFormat: "V:[time(==time_height)]-0-|", options: [], metrics: ["time_height" : 22], views: views);
        
        costraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|-margin-[time]-|", options: .directionLeadingToTrailing, metrics: ["margin" : 42], views: views);
        costraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|-margin-[line(6)]", options: .directionLeadingToTrailing, metrics: ["margin" : 60], views: views);
        self.addConstraints(costraints);
    }

}
